var util = require('../../utils/util.js')
const app = getApp()
let cloudUrl = 'https://video-1306430077.cos-website.ap-chongqing.myqcloud.com'
let baseUrl2 = 'https://zjb.fendou.life'
let baseUrl3 = 'https://www.fendou.life'
// let baseUrl3 = 'https://www.fendou.life/api/common/video?page=0&count=2&keyword=全部'


Page({
  data: {
    form:{},
    indexImg:'',
    jockIsShow:false,
    // 广告数据
    videoAdId: '',
    yuanshengAd1: 'adunit-df84d2891075f223',
    jockList: [],
    video: {},
    videoisshow: false,
    index: null,
    indexlx: null,
    pickerHidden: true,
    chosen: '',
    picker: ['曲靖南片区', '曲靖北片区', '曲靖东片区', '曲靖西片区'],
    pickerlx: ['新房', '二手房'],
    inputValueName: '',
    inputValuePhone: '',
    inputValuebeizhu: ''
  },
  PickerChange(e) {
    this.setData({
      index: e.detail.value
    })
  },
  PickerChangeLx(e) {
    this.setData({
      indexlx: e.detail.value
    })
  },
  formSubmit(e) {
    let data = e.detail.value
    let weizhi = this.data.picker[data.weizhi] 
    let leixin = this.data.pickerlx[data.leixin] 
    let name = data.name
    let phone = data.phone
    let beizhu = data.beizhu
    console.log(weizhi,leixin);
    if (name == '') {
      wx.showToast({
        icon: 'none',
        title: '姓名不能为空',
      })
      return
    }
    if (!this.validatemobile(phone)) {
      return
    }
    // 发送邮件，并把用户信息存入数据库
    wx.cloud.callFunction({
      name: 'sendEmail',
      data: {
        tag: 'fangyuan',
        pingtai:  app.globalData.pingtai_tag,
        weizhi,
        leixin,
        name,
        phone,
        beizhu,
        createTime: util.formatTime(new Date())
      }
    }).then(res => {}).catch(res => {
      wx.showToast({
        icon: 'error',
        title: '查询失败，稍后再试',
      })
    })
    // 存入数据库
    wx.cloud.database().collection('fanyuankehu').add({
      data: {
        pingtai: app.globalData.pingtai_tag,
        weizhi,
        leixin,
        name,
        phone,
        beizhu,
        createTime: util.formatTime(new Date())
      }
    }).then(res => {}).catch(res => {})
    this.formReset()
    if(this.data.form.defult){
      wx.showModal({
        title: '提交成功',
        content: '我们会尽快联系您!',
        cancelColor: 'cancelColor',
      })
    }else{
      wx.showModal({
        title: '提示',
        content: '提交成功',
        cancelColor: 'cancelColor',
      })
    }

  },
  formReset(e) {
    this.setData({
      inputValueName: '',
      inputValuePhone: '',
      inputValuebeizhu: '',
      index: null,
      indexlx: null, 
    })
  },
  onLoad: function () {
    if (app.globalData.SysController == null) {
      app.getController().then(res => {   
        if (res.controller_page.newsVideo) {
          this.setData({
            videoisshow: true,
            videoAdId: res.ad.videoTiepian,
            yuanshengAd1: res.ad.yuansheng1,
            form:res.form
          })
          console.log(this.data.form);
          this.getVideo()
        }else{
          this.setData({
            yuanshengAd1: res.ad.yuansheng1,
            indexImg: res.indexImg
          })
        }
      })
    } 
    wx.showShareMenu({
      withShareTicket: true,
      menus: ['shareAppMessage', 'shareTimeline']
    }) 
    if (!app.globalData.isRegistered) {
      app.selecteUser().then(res => {
        app.globalData.isRegistered = res
      })
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage(e) {
    return {
      title:this.data.video.name,
      imageUrl:this.data.video.poster,
      path: `/pages/news/news`
    }
  },
  onShareTimeline(e) {
    return {
      title:this.data.video.name,
      imageUrl:this.data.video.poster
    }
  },
  // 请求最新的一条视频
  getVideo() {
    // let url = `${baseUrl2}/newslist/status?start=0&count=1&keyWord=全部` 
    let url = `${baseUrl3}/api/common/video?page=0&count=1&keyword=全部`
    wx.request({
      url,
      success: res => {
        console.log(res);
        this.setData({
          video: res.data[0],
          video:{
            videoSrc:cloudUrl+res.data[0].attachfile,
            poster:cloudUrl+res.data[0].smallimage,
            name:res.data[0].name,
          }
        })
      },
      fail: res => {
        this.setData({
          videoisshow: false
        })

      }
    })
  },
  // 检测手机号
  validatemobile(mobile) {
    if (mobile.length == 0) {
      wx.showToast({
        title: '请输入手机号！',
        icon: 'error',
        duration: 2000
      })
      return false;
    }
    if (mobile.length != 11) {
      wx.showToast({
        title: '手机号长度有误！',
        icon: 'error',
        duration: 2000
      })
      return false;
    }
    var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
    if (!myreg.test(mobile)) {
      wx.showToast({
        title: '手机号有误！',
        icon: 'error',
        duration: 2000
      })
      return false;
    }
    return true;
  },

})